package org.blackcat.dao.redis;

import org.blackcat.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

/**
 * Created by Duo Duo on 2017/1/4.
 */
@Component
public class RedisHandle {

    @Autowired
    RedisPools redisPools;


    /**
     * redis getString
     * @param key
     * @return
     */
    public String getString(String key){
        if(StringUtils.isNullOrEmpty(key)){
            return null;
        }
        Jedis jedis = redisPools.getJedisResource();
        String value = jedis.get(key);
        redisPools.returnJedisResource(jedis);
        return value;
    }
    /**
     * redis set String
     */
    public boolean setString(String key,String value,int seconds){
        if(StringUtils.isNullOrEmpty(key) || StringUtils.isNullOrEmpty(value)){
            return false;
        }
        Jedis jedis = redisPools.getJedisResource();
        jedis.setex(key,seconds,value);
        redisPools.returnJedisResource(jedis);
        return true;
    }
}
